﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;

namespace EAGL.Data
{
    /// <summary>
    /// A class used to get workspaces from many types
    /// </summary>
    public class WorkspaceManager
    {
        private WorkspaceInfo wsinfo;
        
        public WorkspaceManager(WorkspaceInfo wsinfo)
        {
            this.wsinfo = wsinfo;
        }

        public IWorkspace GetWorkspace()
        {
            IWorkspace ws = null;
            IWorkspaceFactory factory = null;
            WorkspaceType wstype = wsinfo.Type;
            if (wstype == WorkspaceType.SDE)
            {
               factory = new SdeWorkspaceFactoryClass();
            }
            else if (wstype == WorkspaceType.ACCESS)
            {
                factory = new AccessWorkspaceFactory();
            }
            else if (wstype == WorkspaceType.SHAPE_FILE)
            {
                factory = new ShapefileWorkspaceFactoryClass();
            }
            else if (wstype == WorkspaceType.FILE_GDB)
            {
                factory = new FileGDBWorkspaceFactoryClass();
            }
            
            if (factory == null)
                throw new Exception("No suitable workspace factory");
            
            IPropertySet pset = wsinfo.PropertySet;
            ws =  factory.Open(pset, 0);
            return ws;
        }
    }
}
